﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 钎焊记录Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksBrazingRecordService), ServiceLifetime = LifeTime.Transient)]
    public class CksBrazingRecordService : BaseService<CksBrazingRecord>, ICksBrazingRecordService
    {
        /// <summary>
        /// 查询钎焊记录列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksBrazingRecordDto> GetList(CksBrazingRecordQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderByDescending(a=>a.Id)
                .ToPage<CksBrazingRecord, CksBrazingRecordDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksBrazingRecord GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加钎焊记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksBrazingRecord AddCksBrazingRecord(CksBrazingRecord model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改钎焊记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksBrazingRecord(CksBrazingRecord model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksBrazingRecord> QueryExp(CksBrazingRecordQueryDto parm)
        {
            var predicate = Expressionable.Create<CksBrazingRecord>();
            predicate.AndIF(!parm.BatchNo.IsEmpty(), a => a.BatchNo == parm.BatchNo);
            return predicate;
        }

        public List<CksBrazingDetails> GetDegrees(CksBrazingRecordQueryDto parm)
        {
            return Queryable().Context.Queryable<CksBrazingDetails>()
                 .Where(a => a.BatchNo == parm.BatchNo)
                 .ToList();
        }
    }
}